package com.yonglian.management.web.rest;

import com.yonglian.management.domain.ProjectBaseInfo;
import com.yonglian.management.repository.ProjectBaseInfoRepository;
import com.yonglian.management.service.ProjectBaseInfoService;
import com.yonglian.management.web.rest.errors.BadRequestAlertException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.PaginationUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.yonglian.management.domain.ProjectBaseInfo}.
 */
@RestController
@RequestMapping("/api/project-base-infos")
public class ProjectBaseInfoResource {

    private final Logger log = LoggerFactory.getLogger(ProjectBaseInfoResource.class);

    private static final String ENTITY_NAME = "projectBaseInfo";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final ProjectBaseInfoService projectBaseInfoService;

    private final ProjectBaseInfoRepository projectBaseInfoRepository;

    public ProjectBaseInfoResource(ProjectBaseInfoService projectBaseInfoService, ProjectBaseInfoRepository projectBaseInfoRepository) {
        this.projectBaseInfoService = projectBaseInfoService;
        this.projectBaseInfoRepository = projectBaseInfoRepository;
    }

    /**
     * {@code POST  /project-base-infos} : Create a new projectBaseInfo.
     *
     * @param projectBaseInfo the projectBaseInfo to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new projectBaseInfo, or with status {@code 400 (Bad Request)} if the projectBaseInfo has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("")
    public ResponseEntity<ProjectBaseInfo> createProjectBaseInfo(@Valid @RequestBody ProjectBaseInfo projectBaseInfo)
        throws URISyntaxException {
        log.debug("REST request to save ProjectBaseInfo : {}", projectBaseInfo);
        if (projectBaseInfo.getId() != null) {
            throw new BadRequestAlertException("A new projectBaseInfo cannot already have an ID", ENTITY_NAME, "idexists");
        }
        projectBaseInfo = projectBaseInfoService.save(projectBaseInfo);
        return ResponseEntity.created(new URI("/api/project-base-infos/" + projectBaseInfo.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, projectBaseInfo.getId().toString()))
            .body(projectBaseInfo);
    }

    /**
     * {@code PUT  /project-base-infos/:id} : Updates an existing projectBaseInfo.
     *
     * @param id the id of the projectBaseInfo to save.
     * @param projectBaseInfo the projectBaseInfo to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated projectBaseInfo,
     * or with status {@code 400 (Bad Request)} if the projectBaseInfo is not valid,
     * or with status {@code 500 (Internal Server Error)} if the projectBaseInfo couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/{id}")
    public ResponseEntity<ProjectBaseInfo> updateProjectBaseInfo(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody ProjectBaseInfo projectBaseInfo
    ) throws URISyntaxException {
        log.debug("REST request to update ProjectBaseInfo : {}, {}", id, projectBaseInfo);
        if (projectBaseInfo.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, projectBaseInfo.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!projectBaseInfoRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        projectBaseInfo = projectBaseInfoService.update(projectBaseInfo);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, projectBaseInfo.getId().toString()))
            .body(projectBaseInfo);
    }

    /**
     * {@code PATCH  /project-base-infos/:id} : Partial updates given fields of an existing projectBaseInfo, field will ignore if it is null
     *
     * @param id the id of the projectBaseInfo to save.
     * @param projectBaseInfo the projectBaseInfo to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated projectBaseInfo,
     * or with status {@code 400 (Bad Request)} if the projectBaseInfo is not valid,
     * or with status {@code 404 (Not Found)} if the projectBaseInfo is not found,
     * or with status {@code 500 (Internal Server Error)} if the projectBaseInfo couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<ProjectBaseInfo> partialUpdateProjectBaseInfo(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody ProjectBaseInfo projectBaseInfo
    ) throws URISyntaxException {
        log.debug("REST request to partial update ProjectBaseInfo partially : {}, {}", id, projectBaseInfo);
        if (projectBaseInfo.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, projectBaseInfo.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!projectBaseInfoRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<ProjectBaseInfo> result = projectBaseInfoService.partialUpdate(projectBaseInfo);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, projectBaseInfo.getId().toString())
        );
    }

    /**
     * {@code GET  /project-base-infos} : get all the projectBaseInfos.
     *
     * @param pageable the pagination information.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of projectBaseInfos in body.
     */
    @GetMapping("")
    public ResponseEntity<List<ProjectBaseInfo>> getAllProjectBaseInfos(@org.springdoc.core.annotations.ParameterObject Pageable pageable) {
        log.debug("REST request to get a page of ProjectBaseInfos");
        Page<ProjectBaseInfo> page = projectBaseInfoService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(ServletUriComponentsBuilder.fromCurrentRequest(), page);
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
     * {@code GET  /project-base-infos/:id} : get the "id" projectBaseInfo.
     *
     * @param id the id of the projectBaseInfo to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the projectBaseInfo, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/{id}")
    public ResponseEntity<ProjectBaseInfo> getProjectBaseInfo(@PathVariable("id") Long id) {
        log.debug("REST request to get ProjectBaseInfo : {}", id);
        Optional<ProjectBaseInfo> projectBaseInfo = projectBaseInfoService.findOne(id);
        return ResponseUtil.wrapOrNotFound(projectBaseInfo);
    }

    /**
     * {@code DELETE  /project-base-infos/:id} : delete the "id" projectBaseInfo.
     *
     * @param id the id of the projectBaseInfo to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteProjectBaseInfo(@PathVariable("id") Long id) {
        log.debug("REST request to delete ProjectBaseInfo : {}", id);
        projectBaseInfoService.delete(id);
        return ResponseEntity.noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, id.toString()))
            .build();
    }
}
